package com.example.ffmpegplayer.player;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.example.ffmpegplayer.view.MediaGLSurfaceView;

/**
 * @author ly-zhouxyi
 * @date 2024/3/4
 * @time 16:23
 */
public class PlayerManager {
    private static final String TAG = PlayerManager.class.getSimpleName();
    private static volatile PlayerManager mInstance;
    private IPlayerCallback mIPlayerCallback;

    private MediaGLSurfaceView mediaGLSurfaceView;

    private int duration;

    private final Handler mHandler = new Handler(Looper.getMainLooper());

    static {
        System.loadLibrary("ffmpegplayer");
    }

    public PlayerManager() {
    }

    public static PlayerManager getInstance() {
        if (mInstance == null) {
            synchronized (PlayerManager.class) {
                if (mInstance == null) {
                    mInstance = new PlayerManager();
                }
            }
        }
        return mInstance;
    }

    public int getDuration() {
        return duration;
    }

    void setSurfaceView(MediaGLSurfaceView surfaceView) {
        this.mediaGLSurfaceView = surfaceView;
    }

    public void registerIPlayerCallback(IPlayerCallback playerCallback) {
        this.mIPlayerCallback = playerCallback;
    }

    public void unregisterIPlayerCallback() {
        this.mIPlayerCallback = null;
    }

    public void onPrepare() {
        Log.d(TAG, "onPrepare  ");
        if (mIPlayerCallback != null) {
            mIPlayerCallback.onParpared();
        }
    }

    public void onLoad(boolean load) {
        Log.d(TAG, "load  " + load);
        if (mIPlayerCallback != null) {
            mHandler.post(() -> mIPlayerCallback.onLoad(load));
        }
    }

    public void onPlaying(boolean isPlaying) {
        Log.d(TAG, "isPlaying  " + isPlaying);
        if (mIPlayerCallback != null) {
            mHandler.post(() -> mIPlayerCallback.onPlaying(isPlaying));
        }
    }

    public void onRenderYUV(int width, int height, byte[] y, byte[] u, byte[] v) {
        if (mediaGLSurfaceView != null) {
            mediaGLSurfaceView.setYUVData(width, height, y, u, v);
        }
    }

    public void onTimeInfo(int currentTime, int totalTime) {
        this.duration = totalTime;
        if (mIPlayerCallback != null) {
            mHandler.post(() ->   mIPlayerCallback.onCurrentTime(currentTime,totalTime));
        }
    }


    public native void native_init();

    public native void native_setDataSource(String url);

    public native void native_prepare();

    public native void native_start();

    public native void native_stop();

    public native void native_pause();

    public native void native_resume();

    public native void native_setVolume(int volume);

    public native void native_setSpeed(float speed);

    public native void native_setPitch(float pitch);

    public native void native_setVolumeMute(boolean mute);

    public native void native_setChannelMute(int chan);

    public native void native_seek(int second);

    public native int native_getVolume();
}